package com.zhao.controller.show;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/so")
public class SearchController {
    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;
    @Autowired
    DatafileService datafileService;
    @Autowired
    WalletService walletService;
    @Autowired
    FollowService followService;

    @RequestMapping("/search")
    public String SearchAll(@RequestParam(value = "like",required = false)String like,
                            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(value = "searchType",required = false,defaultValue = "boke")String searchType,
                            Model model){

        //热门文章5
        List<Article> hots=articleService.host(5);
        //查询文章
        if (searchType.equals("boke")){
            Page<Article> articlePages = articleService.selectListPage(pageNum, 8, like, null, "1");
            model.addAttribute("articlePages",articlePages);
        }
        //查询资源
        if (searchType.equals("ziyuan")){
            Page<Datafile> datafilePages = datafileService.selectListPage(pageNum, 1, like, null, "1");
            model.addAttribute("datafilePages",datafilePages);
        }
        //查询用户
        if (searchType.equals("users")){
            QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("status", "正常");
            if (like!=null&&!like.equals("")) {
                wrapper.like("user_account", like);
            }
            List<User> users = userService.list(wrapper);
            if (users!=null){
                User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
                for (User user:users) {
                    Statistic statistic = userService.SumStatistic(user.getUserAccount());
                    if (statistic.getZan()==null)statistic.setZan(0);
                    if (statistic.getHits()==null)statistic.setHits(0);
                    user.setStatistic(statistic);
                    Wallet wallet = walletService.getOne(new QueryWrapper<Wallet>().eq("uid", user.getUserId()));
                    user.setWallet(wallet);
                    if (loginUser!=null){
                        Follow follow = followService.getOne(new QueryWrapper<Follow>().eq("uid", loginUser.getUserId()).eq("fid", user.getUserId()));
                        if (follow!=null){
                            user.setFollow("已关注");
                        }else {
                            user.setFollow("关注");
                        }
                    }else {
                        user.setFollow("关注");
                    }
                }
            }
            model.addAttribute("users",users);
        }
        if (Objects.isNull(like)){
            like="";
        }
        model.addAttribute("searchType",searchType);
        model.addAttribute("like",like);
        model.addAttribute("hots",hots);
        return "show/searchAll";
    }
    @ResponseBody
    @RequestMapping("/isFollow/{fid}")
    public String isfollow(@PathVariable String fid){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        return followService.isfollow(fid,loginUser);
    }
}
